package com.datasecops.scannerservices.operation;

import com.datasecops.scannerservices.db.Redis;
import com.datasecops.scannerservices.entity.CommonParam;
import com.datasecops.scannerservices.service.ScanDataProcessServers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author: GeXiaoLong
 * @time: 2022/10/13 14:05
 */
@Service
@Slf4j
public class RedisOperation {

    //每一批次定1000行
    private int batchLine = 1000;

    @Resource
    private ScanDataProcessServers dataProcessServers;


    public List<String> getTablesByDBName(CommonParam param, String Instance, String Model){
        return Redis.getAllCollections(param.getIp(), param.getPort(),param.getPassword());
    }


    /**
     * 获取DB数据返回
     *
     * @param
     * @param commonParam
     * @param jobId
     * @param jobName
     * @param scannerID
     * @param linage
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/16 10:43
     */
    public boolean getDBResult(CommonParam param, String Instance, CommonParam commonParam, String jobId, String jobName, String scannerID, String linage) {
        //连接本地的redis
        Jedis jedis = new Jedis(param.getIp(), param.getPort());
        jedis.connect();

        try {
            if (!(param.getPassword().equalsIgnoreCase("nouserdefault")) ){
                //如果有密码则需要下面这一行
                jedis.auth(param.getPassword());
            }
            //查看服务是否运行,运行正常的话返回一个PONG，否则返回一个连接错误
            jedis.ping();
            int libName = Integer.parseInt(Instance);
            jedis.select(libName);
            Set keys = jedis.keys("*");
            System.out.println(keys);
            List<String> list = new ArrayList<String>();
            List<String> lists = new ArrayList<String>();
            Redis redis = new Redis();
            int count = 0;
            int batch = 0;
                for (Object key : keys) {
                    String type = jedis.type((String) key);
                    if ("string".equalsIgnoreCase(type)) {
                        Object o = jedis.get((String) key);
                        if (o == null) {
                            o = "";
                        }
                        list.add(o.toString());

                        Integer size = redis.valueSize(o.toString());
                        count += size;
                        String data = size + "|";
                        lists.add(data);
                        if (count == (1024*1024*100)) {//每100M大小，为一个批次
                            //数据操作
                            batch++;
                            //todo

                            dataProcessServers.tradingData(param, commonParam,list, batch, true, "key_data_", ".dat",jobId,jobName,scannerID,linage);
                            dataProcessServers.tradingData(param, commonParam, lists, batch, false, "key_data_index_", ".txt",jobId,jobName,scannerID, linage);
                            //sourceDataMap.clear();
                        }
                    }
                }



            //处理未满一千行的
            if (list.size() > 0) {
                batch++;
                dataProcessServers.tradingData(param, commonParam, list,batch,true,"key_data_",".dat", jobId, jobName, scannerID, linage);
                dataProcessServers.tradingData(param, commonParam, lists,batch,false,"key_data_index_",".txt", jobId, jobName, scannerID, linage);
                //sourceDataMap.clear();
                list.clear();
            }

        } catch (Exception e) {
            log.error("Failed Method getDBResult error :" + e.getMessage());
            System.out.println("服务运行失败");
            return false;
        } finally {
            Redis.close();
        }
        return true;
    }
}
